package com.hankol.newretail.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.utils.GlideUtils;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.view.GlideRoundTransform;
import com.baselibrary.view.RoundCornerImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hankol.newretail.R;
import com.baselibrary.bean.MediaInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 酒窖编辑--上传图片、视频
 *
 * @author: merbng
 * @time: 2022/7/6 12:19
 * @description:
 */
public class EvaluationUploadImageVideoAdapter extends RecyclerView.Adapter<EvaluationUploadImageVideoAdapter.ImageVideoHolder> {
    private Context mContext;
    private List<MediaInfo> mList = new ArrayList<>();
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public EvaluationUploadImageVideoAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<MediaInfo> getListData() {
        return mList;
    }

    public void setData(List<MediaInfo> list) {
        this.mList = list;
    }

    public void addData(List<MediaInfo> list) {
        this.mList.addAll(list);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public EvaluationUploadImageVideoAdapter.ImageVideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new EvaluationUploadImageVideoAdapter.ImageVideoHolder(mInflater.inflate(R.layout.item_upload_image_and_video, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull EvaluationUploadImageVideoAdapter.ImageVideoHolder imageVideoHolder, int position) {
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) imageVideoHolder.rlRoot.getLayoutParams();
        params.width = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPx(71))/3;
        params.height = params.width;
        imageVideoHolder.rlRoot.setLayoutParams(params);
        if (position < mList.size()) {
            MediaInfo coverBean = mList.get(position);
            //代表+号之前的需要正常显示图片
            imageVideoHolder.mIvCoversType.setVisibility(coverBean.getType() == 1 ? View.VISIBLE : View.GONE);
            String picUrl = coverBean.getType() == 0 ? coverBean.getCoverUrl() : coverBean.getVideoUrl(); //图片路径
            GlideUtils.loadImg_101(mContext, picUrl, imageVideoHolder.mIvFriendlistHead);
            if (mList.size() >= 1) {
                imageVideoHolder.mIvDelete.setVisibility(View.VISIBLE);
            } else {
                imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            }
            imageVideoHolder.listener(imageVideoHolder, position);
        } else {
            //添加按钮
            imageVideoHolder.mIvCoversType.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(R.drawable.img_up_load)
                    .centerCrop()
                    .placeholder(R.drawable.ic_hankol_101)
                    .error(R.drawable.ic_hankol_101)
                    .transform(new GlideRoundTransform(mContext, 8))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageVideoHolder.mIvFriendlistHead);
            imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                //拖拽Item 这里不添加任何点击事件。。但是不实现长按监听，则默认执行拖拽方法，所以这里面什么都不做
                return false;
            });
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onAddItemClick();
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = mList == null || mList.size() == 0 ? 1 : mList.size() == 9 ? mList.size() : mList.size() + 1;
        return count;
    }

    public class ImageVideoHolder extends RecyclerView.ViewHolder {
        private RoundCornerImageView mIvFriendlistHead;
        private ImageView mIvCoversType;
        private LinearLayout mIvDelete;
        private RelativeLayout rlRoot;

        public ImageVideoHolder(@NonNull View itemView) {
            super(itemView);
            mIvFriendlistHead = itemView.findViewById(R.id.pic_iv_new);
            mIvDelete = itemView.findViewById(R.id.iv_delete);
            mIvCoversType = itemView.findViewById(R.id.iv_covers_type);
            rlRoot = itemView.findViewById(R.id.rl_root);
        }

        private void listener(@NonNull ImageVideoHolder imageVideoHolder, int position) {
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(position, mList);
            });
            imageVideoHolder.mIvDelete.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onDeleteItemClick(position, mList);
            });
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int pos, List<MediaInfo> list);

        void onAddItemClick();

        void onDeleteItemClick(int pos, List<MediaInfo> list);
    }
}

